package com.example.home.volunteer_service.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.example.home.volunteer_service.adapter.ServiceNewsAdapter;
import com.example.home.volunteer_service.adapter.VolunteerBannerAdapter;
import com.example.home.volunteer_service.bean.ServiceNewsBean;
import com.example.home.volunteer_service.bean.VolunteerBannerBean;

import java.util.Timer;
import java.util.TimerTask;

public class ActivityFragment extends Fragment {
    private ImageView back;
    private ViewPager vpBanner;
    private RecyclerView rcvList;

    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private View view;
    private VolunteerBannerAdapter volunteerBannerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_activity, container, false);


        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        vpBanner = view.findViewById(R.id.vp_banner);
        rcvList = view.findViewById(R.id.rcv_list);

        //轮播图
        getBanner();
        startAutoScroll();

        getNewsData();

        return view;
    }

    private void getNewsData() {
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/news/list",
                OKHttpTool.GET,
                ServiceNewsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        ServiceNewsBean serviceNewsBean = (ServiceNewsBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindNewsData(serviceNewsBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindNewsData(ServiceNewsBean serviceNewsBean) {
        ServiceNewsAdapter serviceNewsAdapter = new ServiceNewsAdapter(getActivity(),serviceNewsBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rcvList.setAdapter(serviceNewsAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/ad-banner/list",
                OKHttpTool.GET,
                VolunteerBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        VolunteerBannerBean volunteerBannerBean = (VolunteerBannerBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(volunteerBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannershow(VolunteerBannerBean volunteerBannerBean) {
        ViewPager viewPager = view.findViewById(R.id.vp_banner);
        volunteerBannerAdapter = new VolunteerBannerAdapter(getActivity(), volunteerBannerBean.getData());
        viewPager.setAdapter(volunteerBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == volunteerBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

}